﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class employee : System.Web.UI.Page
{
    DBClass _db = new DBClass();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
            Load_Department();
        }
    }
    /**
        * [Get From Date] get from date.
        * */
    private DateTime fromDate(string fromTime)
    {
        DateTime fromT = DateTime.MinValue;
        DateTime.TryParse(fromTime, out fromT);
        if (fromT == DateTime.MinValue)
            fromT = DateTime.Parse("1/1/1753 12:00:00 AM");
        return fromT;
    }
    /**
    * [Get TO Date] get to date.
    * */
    private DateTime toDate(string toTime)
    {
        DateTime toT;
        DateTime.TryParse(toTime, out toT);
        if (toT == DateTime.MinValue)
            toT = DateTime.MaxValue;
        return toT;
    }
    /**
       * [Filter] filter data with datetime selected.
       * */
    private void LoadData()
    {
        DataTable dt = new DataTable();
        dt = _db.GetList_Employee(null, null, null, null, null, null, null, null, null, null);
        grvData.DataSource = dt;
        grvData.DataBind();
        lbTotal.Text ="Total: "+ dt.Rows.Count.ToString();
    }
    private void Load_Department()
    {

        ddlDepartment.DataSource = _db.GetList_Department();
        ddlDepartment.DataBind();
        ListItem item = new ListItem("----------select---------","");
        ddlDepartment.Items.Insert(0, item);
    }
    private void Load_Section()
    {
        string item = ddlDepartment.SelectedValue.Replace(" ", "");
        DataTable dt= _db.GetList_Section(item);
        
        ddlSectionName.DataSource = dt;
            
        ddlSectionName.DataBind(); 
    }
   
    protected void grvData_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
       
    }
    protected void grvData_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }
    protected void grvData_PageIndexChanged(object sender, EventArgs e)
    {
       
    }
    protected void grvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvData.PageIndex = e.NewPageIndex;
        LoadData();
    }
    protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        Load_Section();
    }
    protected void btnFilter_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        DateTime fromJoin =fromDate(txtJoinDateFrom.Text);
        DateTime toJoin = toDate(txtJoinDateTo.Text);
        DateTime fromBirth = fromDate(txtBirthDayFrom.Text);
        DateTime toBirth = toDate(txtBirthDayTo.Text);

        dt = _db.GetList_Employee(null, txtEmpCode.Text, txtFirstName.Text, txtLastName.Text, ddlDepartment.SelectedValue.Replace(" ", ""), ddlSectionName.SelectedValue.Replace(" ", ""), fromJoin,toJoin,fromBirth, toBirth);
        grvData.DataSource = dt;
        grvData.DataBind();
        lbTotal.Text = "Total: " + dt.Rows.Count.ToString();
    }
}